home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / OOPTUT34.ZIP / RIGHTOOP.PAS < prev    next >
Pascal/Delphi Source File  |  1993-05-31  |  3KB  |  105 lines

  1. program right_oop;
  2.  
  3. {~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  4. { Program to illustrate the use of Virtual Methods in         }
  5. { object-oriented programming. Compare the behaviour of this  }
  6. { program with the program WRONGOOP.PAS which does not use    }
  7. { virtual methods.                                            }
  8. {                                                             }
  9. { RIGHTOOP.PAS  ->  RIGHTOOP.EXE   R. Shaw  26.4.91   31.5.93 }
  10. {_____________________________________________________________}
  11.  
  12. uses Crt;
  13.  
  14. type
  15.  
  16. staff = object
  17.       Name : string;
  18.       constructor Init(SurName : string );
  19.       procedure script1;  virtual;        {virtual procedures which are   }
  20.       procedure script2;  virtual;        {overridden in the object junior}
  21.       procedure action1;            {static procedures which are inherited}
  22.       procedure action2;            {by the descendant object junior.     }
  23.       end;
  24.  
  25. junior = object( staff )
  26.            procedure script1;  virtual;     {virtual procedures which     }
  27.            procedure script2;  virtual;     {override inherited procedures}
  28.          end;
  29.  
  30. var
  31.    LetterName : string;
  32.  
  33. constructor staff.Init( SurName : string );
  34. begin
  35.      Name := SurName;
  36. end;
  37.  
  38. procedure staff.script1;
  39. begin
  40.      writeln( Name,
  41.          ': Please send the duplicated letters to all our customers.');
  42.      writeln( '        If you have a problem, please ask me.');
  43. end;
  44.  
  45. procedure staff.script2;
  46. begin
  47.      LetterName := 'OD';
  48.      writeln(Name,': The letter is headed ',LetterName,'.');
  49.      writeln;
  50. end;
  51.  
  52. procedure staff.action1;
  53. begin
  54.      script1;
  55. end;
  56.  
  57. procedure staff.action2;
  58. begin
  59.      script2;
  60. end;
  61.  
  62.  
  63. procedure junior.script1;
  64. begin
  65.      writeln;
  66.      writeln( '< later >');
  67.      writeln;
  68.      writeln( Name, ': I do not know the letter to send for an overdraft.');
  69.      writeln;
  70. end;
  71.  
  72. procedure junior.script2;
  73. begin
  74.      writeln( Name, ': Thank you. ');
  75.      writeln;
  76.      writeln( '< later still >' );
  77.      writeln;
  78.      writeln( Name, ': As you requested, I sent the letter headed ',LetterName,'.');
  79. end;
  80.  
  81. {Main}
  82.  
  83. var
  84.    Young      : junior;
  85.    Old        : staff;
  86.  
  87. begin
  88.      clrscr;
  89.  
  90.      Young.Init( 'NEWMAN' );
  91.      Old.Init( 'SENIOR' );
  92.  
  93.      Old.action1;
  94.      Young.action1;
  95.      Old.action2;
  96.      Young.action2;
  97.  
  98.      gotoXY(10,24);
  99.      write('Press any key to conclude: ');
  100.      repeat until keypressed;
  101.      clrscr;
  102. end.
  103.  
  104. { end of listing }
  105.